package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import com.qf.mvc01.utils.MyUserMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public void test1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String value = req.getParameter("value");
        String s = MyUserMap.selectUser(value);
        resp.getWriter().print(s);
    }

    @RequestMapping("/admin")
    public Map<String,String> admin(String uesrname,String phone ,String pass,String kcap, HttpSession session){
        String captcha = (String) session.getAttribute("captcha");
        String s = kcap.toLowerCase();
        String s1 = captcha.toLowerCase();
        String s2 = MyUserMap.selectUser(uesrname);
        Map<String, String> map = new HashMap<>();
        if (s.equals(s1)&&s2.equals("1")){
            MyUserMap.addUser(uesrname,new User(uesrname,phone,pass));
            map.put("code", "1");
            map.put("msg", "登入成功");
        }else {
            map.put("code", "-1");
            map.put("msg", "验证码有误或用户名重复");
        }

        return map;
    }


}
